#define _CRT_SECURE_NO_WARNINGS 1

#include "List.h"

LTNode* BuyLTNode(LTDataType data)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	assert(node);
	node->data = data;
	node->prev = node->next = NULL;

	return node;
}

LTNode* LTInit()
{
	LTNode* phead = BuyLTNode(-1);
	phead->prev = phead->next = phead;

	return phead;
}
void LTDestroy(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	LTNode* next = NULL;
	while (cur != phead)
	{
		next = cur->next;
		free(cur);
		cur = next;
	}

	free(phead);
}
void LTPrint(LTNode* phead)
{
	assert(phead);
	
	printf("<=head=>");
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
bool LTEmpty(LTNode* phead)
{
	assert(phead);

	return phead->next == phead;
}

void LTPushBack(LTNode* phead, LTDataType data)
{
	assert(phead);

	LTNode* newnode = BuyLTNode(data);

	LTNode* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}
void LTPopBack(LTNode* phead)
{
	assert(phead);
	if (phead->next != phead)
	{
		LTNode* tail = phead->prev;
		tail->prev->next = phead;
		phead->prev = tail->prev;

		free(tail);
	}
}

void LTPushFront(LTNode* phead, LTDataType data)
{
	asseert(phead);

	LTNode* newnode = BuyLTNode(data);
	LTNode* first = phead->next;

	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = head;
	head->prev = newnode;
}
void LTPopFront(LTNode* phead);

void LTInsert(LTNode* pos, LTDataType data);
void LTErase(LTNode* pos);